package com.gzbugu.bugusys.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.dao.IHQLQuery;
import com.gzbugu.bugusys.service.ICoCertificateService;
import com.gzbugu.bugusys.util.PageBean;

@Transactional
@Service
public class CoCertificateService implements ICoCertificateService {

	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;
	@Resource(name = "hqlQuery")
	IHQLQuery hqlQuery;

	/**
	 * 分页查询
	 * 
	 * @param currentPage
	 *            当前第几页
	 * @param pageSize
	 *            每页大小
	 * @return 封闭了分页信息(包括记录集list)的Bean
	 */
	public PageBean queryForPage(int pageSize, int page) {

		int allRow = baseDAO
				.countRow(com.gzbugu.bugusys.fields.VoClasseFields.CO_CERTIFICATE); // 总记录数
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		final int currentPage = PageBean.countCurrentPage(page);
		List list = baseDAO
				.findAll(com.gzbugu.bugusys.fields.VoClasseFields.CO_CERTIFICATE);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	@Override
	public PageBean queryForPageHQL(int pageSize, int page, short coId) {

		String hql = "from CoCertificate coCertificate where coCertificate.type.id =:typeid  order by coCertificate.type.id,coCertificate.uploadDate desc";
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		int allRow = ((Long) hqlQuery
				.find("select count(*) from CoCertificate").get(0)).intValue(); // 总记录数
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		final int currentPage = PageBean.countCurrentPage(page);
		List list = new ArrayList();
		if (coId == 0) {
			hql = "from  CoCertificate coCertificate";// 查询全部
			list = hqlQuery.find(hql, offset, pageSize);
		} else if (coId == 1) {
			paramsMap.put("typeid", new Short((short) 5));
			list = hqlQuery.find(hql, paramsMap, offset, pageSize);
		} else if (coId == 2) {
			paramsMap.put("typeid", new Short((short) 6));
			list = hqlQuery.find(hql, paramsMap, offset, pageSize);
		} else if (coId == 3) {
			paramsMap.put("typeid", new Short((short) 4));
			list = hqlQuery.find(hql, paramsMap, offset, pageSize);
		}

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	@Override
	public File saveUploadPicture(String pictureFileName, File picture) {
		if (pictureFileName == null || picture == null)
			return null;
		// 资质证书文件保存在根目录下的filesources/coCertificate文件夹下

		File saved = new File(ServletActionContext.getServletContext()
				.getRealPath("/filesources/coCertificate"), pictureFileName);
		InputStream ins = null;
		OutputStream ous = null;
		try {
			saved.getParentFile().mkdir();

			ins = new FileInputStream(picture);
			ous = new FileOutputStream(saved);

			byte[] b = new byte[1024];
			int len = 0;
			while ((len = ins.read(b)) != -1) {
				ous.write(b, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (ous != null)
				try {
					ous.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (ins != null)
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return saved;
	}

}
